﻿#region Using namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml;

using QuickGraph;
using QuickGraph.Serialization;

using BOL.IO;

#endregion

namespace BOL.Apps.GeneNetworkBrowser.IO
{
    public class GraphMLWriter : IWriter
    {
        #region Private variables

        private Network _network;
        private FileStream _fs;

        #endregion

        #region Constructors

        public GraphMLWriter(Network network, FileStream fs)
        {
            _network = network;
            _fs = fs;
        }

        public GraphMLWriter(Network network, string path)
            : this(network, new FileStream(path, FileMode.Create)) { }

        #endregion

        #region IWriter implementation

        public bool Write()
        {
            using (var xw = XmlWriter.Create(_fs))
            {
                _network.SerializeToGraphML<Gene, Relation, Network>(
                    xw, 
                    gene => gene.Id.ToString(),
                    relation => ((uint)relation.RelationTypes).ToString()
                );

                return true;
            }
        }

        public void Close()
        {
            _fs.Close();
        }

        #endregion
    }
}
